﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using Billing.DataObjects;
using System.Reflection;
using System.IO;

namespace Billing.Windows.Shell.SystemObjects
{
    public partial class ShellForms : BaseList
    {
        ShellDataContext mShellDataContext = new ShellDataContext();

        #region Constructors
        public ShellForms()
            : base(true)
        {
            InitializeComponent();
        }

        public ShellForms(bool Register)
            : base(Register)
        {
            InitializeComponent();
        }

        #endregion

        #region Overrides

        protected override void onDataLoad()
        {
            base.onDataLoad();
            this.bsElement.DataSource = mShellDataContext.Forms;
            this.ElementRowTemplate = ultraGridRowEditTemplate1;
        }
        protected override void onElementInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            base.onElementInitializeLayout(sender, e);
            grd.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect;
            if (e.Layout.Bands[0].Columns.Count > 0)
            {
                e.Layout.Bands[0].Columns["GUID"].Hidden = true;
                e.Layout.Bands[0].Columns["ModifierID"].Hidden = true;
                e.Layout.Bands[0].Columns["ModifiedDate"].Hidden = true;
                e.Layout.Bands[0].Columns["DLLID"].Hidden = true;
                e.Layout.Bands[0].Columns["statusID"].Hidden = true;

                e.Layout.Bands[0].Columns["ID"].Header.Caption = "ინდექსი";
                e.Layout.Bands[0].Columns["Key"].Header.Caption = "უნიკალური ველი";
                e.Layout.Bands[0].Columns["Name"].Header.Caption = "სახელი";
                e.Layout.Bands[0].Columns["CreatorID"].Header.Caption = "შემქმნელი";
                e.Layout.Bands[0].Columns["CreatedDate"].Header.Caption = "შექმნის თარიღი";
                e.Layout.Bands[0].Columns["SID"].Header.Caption = "სორტირების ინდექსი";
                e.Layout.Bands[0].Columns["PATH"].Header.Caption = "გზა";
                e.Layout.Bands[0].Columns["Dll"].Header.Caption = "ბიბლიოთეკა";
                e.Layout.Bands[0].Columns["Dll"].Header.VisiblePosition = 3;
            }
        }
        protected override void onAfterInitialize()
        {
            base.onAfterInitialize();

            this.ElementNameID = "FormID";
        }
        protected override void onDelete()
        {
            try
            {
                base.onDelete();
                bsElement.RemoveCurrent();
                //using (StandardBank.DataAccess.FormDB objDB = new StandardBank.DataAccess.FormDB())
                //{
                //    objDB.delete(this.getCurrentElementRowID);
                //}
                this.onDataLoad();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        protected override void onSave()
        {
            base.onSave();
            mShellDataContext.SubmitChanges();
        }
        protected override void onClose()
        {
            if (AllowSave && (mShellDataContext.GetChangeSet().Deletes.Count > 0
                || mShellDataContext.GetChangeSet().Updates.Count > 0
                || mShellDataContext.GetChangeSet().Inserts.Count > 0))
            {
                if (MessageBox.Show("შევინახო შეცვლილი მონაცემები?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    mShellDataContext.SubmitChanges();
            }
            base.onClose();
        }

        #endregion

        private void btnTemplateOk_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and save any pending changes.
            if (comboBox1.SelectedIndex == 0)
            {
                grd.Selected.Rows[0].Cells["StatusID"].Value = 1;
            }
            else
            {
                grd.Selected.Rows[0].Cells["StatusID"].Value = 0;
            }
            this.ultraGridRowEditTemplate1.Close(true);
        }

        private void btnTemplateCancel_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and discard any pending changes.
            this.ultraGridRowEditTemplate1.Close(false);
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            DLLsForm form = new DLLsForm(false);
            form.AllowAdd = false;
            form.AllowDelete = false;
            form.AllowEdit = false;
            form.AllowSave = false;
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                DLL sdll = ((DLL)form.SelectedObjects[0].CurrentObject);
                IEnumerable<DLL> dlls = from n in mShellDataContext.DLLs
                                        where n.ID == sdll.ID
                                        select n;
                foreach (DLL d in dlls)
                {
                    (this.ultraGridRowEditTemplate1.Row.ListObject as Billing.DataObjects.Form).DLL = d;
                }
                //this.ultraGridRowEditTemplate1.Row.Cells["dllID"].Value = .ID;
            }
        }

        private void ultraGridRowEditTemplate1_Enter(object sender, EventArgs e)
        {
            if (Convert.ToInt32(grd.Selected.Rows[0].Cells["StatusID"].Value) == 0)
            {
                comboBox1.SelectedIndex = 1;
            }
            else
            {
                comboBox1.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ParseDLLForm form = new ParseDLLForm(false);
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                FormInfo fi = form.SelectedObjects[0].CurrentObject as FormInfo;
                grd.Selected.Rows[0].Cells["Key"].Value = fi.Key;
                grd.Selected.Rows[0].Cells["Path"].Value = Convert.ToString(fi.Value);
                grd.Selected.Rows[0].Cells["Name"].Value = fi.Name;
            }
        }
    }
}
